﻿#include "HNAlertView.h"
#include "InputField.h"

#define BUTTON_HEIGHT 40
#define PADDING 10

#include <string>
using namespace std;

bool HNAlertView::init(){
	setContentSize(ALERTVIEW_SIZE);
	setAnchorPoint(ccp(0.5, 0.5));
	delegate = 0;
	isBecomeFirstResponder = false;

	int inset = 3;

	backgroundSprite = CCScale9Sprite::createWithSpriteFrameName("AlertBackgroundSprite.png");
	backgroundSprite->setCapInsets(CCRectMake(inset, inset, backgroundSprite->getContentSize().width - 2*inset, backgroundSprite->getContentSize().height - 2*inset));
	backgroundSprite->setPreferredSize(ALERTVIEW_SIZE);
	backgroundSprite->setAnchorPoint(ccp(0, 0));
	addChild(backgroundSprite);

	messageLabel = CCLabelTTF::create("", "", 20);
	messageLabel->setDimensions(CCSizeMake(380, 0));
	messageLabel->setHorizontalAlignment(kCCTextAlignmentCenter);
	messageLabel->setPosition(ccp(backgroundSprite->getContentSize().width/2, backgroundSprite->getContentSize().height - 40));
	backgroundSprite->addChild(messageLabel);

	float inputFieldWidth = 380;
	inputField = new InputField();
	inputField->init(CCRectMake(messageLabel->getPositionX() - 30, messageLabel->boundingBox().getMinY() - 80, inputFieldWidth, 40), "", 20, 200);
	inputField->editBox->setText("000");
	inputField->setVisible(false);
	this->addChild(inputField);
	
	CCScale9Sprite* leftButtonSprite = CCScale9Sprite::createWithSpriteFrameName("AlertViewButton.png");
	CCScale9Sprite* leftButtonSelectedSprite = CCScale9Sprite::createWithSpriteFrameName("AlertViewButtonSelected.png");
	CCScale9Sprite* rightButtonSprite = CCScale9Sprite::createWithSpriteFrameName("AlertViewButton.png");
	CCScale9Sprite* rightButtonSelectedSprite = CCScale9Sprite::createWithSpriteFrameName("AlertViewButtonSelected.png");
	
	leftButtonSprite->setCapInsets(CCRectMake(inset, inset, leftButtonSprite->getContentSize().width - 2*inset, leftButtonSprite->getContentSize().height - 2*inset));
	leftButtonSelectedSprite->setCapInsets(CCRectMake(inset, inset, leftButtonSprite->getContentSize().width - inset, leftButtonSprite->getContentSize().height - 2*inset));
	rightButtonSprite->setCapInsets(CCRectMake(inset, inset, rightButtonSprite->getContentSize().width - 2*inset, rightButtonSprite->getContentSize().height - 2*inset));
	rightButtonSelectedSprite->setCapInsets(CCRectMake(inset, inset, rightButtonSelectedSprite->getContentSize().width - inset, rightButtonSelectedSprite->getContentSize().height - 2*inset));

	float buttonWidth = (backgroundSprite->getContentSize().width - 3*PADDING)/2;

	leftButton = CCControlButton::create(leftButtonSprite);
	leftButton->setBackgroundSpriteForState(leftButtonSelectedSprite, CCControlStateHighlighted);
	leftButton->setPreferredSize(CCSizeMake(buttonWidth, BUTTON_HEIGHT));
	leftButton->setPosition(ccp(PADDING + buttonWidth/2, PADDING + BUTTON_HEIGHT/2));
	leftButton->addTargetWithActionForControlEvents(this, cccontrol_selector(HNAlertView::leftButtonTouched), CCControlEventTouchUpInside);
	//leftButton->setTitleForState(CCString::create(string("Cancel")), CCControlStateNormal);
	leftButton->setTitleTTFSizeForState(20, CCControlStateNormal);
	backgroundSprite->addChild(leftButton, 1);

	rightButton = CCControlButton::create(rightButtonSprite);
	rightButton->setBackgroundSpriteForState(rightButtonSelectedSprite, CCControlStateHighlighted);
	rightButton->setPreferredSize(CCSizeMake(buttonWidth, BUTTON_HEIGHT));
	rightButton->setPosition(ccp(PADDING + buttonWidth + PADDING + buttonWidth/2, PADDING + BUTTON_HEIGHT/2));
	rightButton->addTargetWithActionForControlEvents(this, cccontrol_selector(HNAlertView::rightButtonTouched), CCControlEventTouchUpInside);
	rightButton->setTitleTTFSizeForState(20, CCControlStateNormal);
	backgroundSprite->addChild(rightButton, 1);

	return true;
}

HNAlertView* HNAlertView::create(char* message, char* leftButtonString, char* rightButtonString, char* inputLabelString, HNAlertViewDelegate* delegate){
	HNAlertView* alertView = HNAlertView::create();
	alertView->leftButton->setTitleForState(CCString::create(string(leftButtonString)), CCControlStateNormal);
	alertView->rightButton->setTitleForState(CCString::create(string(rightButtonString)), CCControlStateNormal);
	alertView->messageLabel->setString(message);
	alertView->setDelegate(delegate);

	if(inputLabelString){
		alertView->inputField->setVisible(true);
		alertView->inputField->label->setString(inputLabelString);
	}
	return alertView;
}

void HNAlertView::show(CCNode* node){
	node->addChild(this);
	setPosition(node->getContentSize().width/2, node->getContentSize().height/2);

	setScale(0.4f);
	CCScaleTo* scaleTo = CCScaleTo::create(0.1, 1.2);
	CCScaleTo* scaleTo2 = CCScaleTo::create(0.1, 0.9);
	CCScaleTo* scaleTo3 = CCScaleTo::create(0.1, 1.0);
	CCCallFunc* callFuncAction = CCCallFunc::create(this, callfunc_selector(HNAlertView::endShowAction));
	CCSequence* sequence = CCSequence::create(CCArray::create(scaleTo, scaleTo2, scaleTo3, callFuncAction, 0));
	runAction(sequence);
}

void HNAlertView::endShowAction(){
	if(isBecomeFirstResponder == true){
		inputField->editBox->touchDownAction(inputField->editBox, CCControlEventTouchUpInside);
	}
}

void HNAlertView::setDelegate(HNAlertViewDelegate* delegate){
	this->delegate = delegate;
}

void HNAlertView::leftButtonTouched(CCObject *sender, CCControlEvent controlEvent){
	char* inputString = 0;
	if(inputField->isVisible()){
		inputString = (char*)inputField->editBox->getText();
	}
	if(delegate)
		delegate->alertViewButtonTouched(0, inputString);
	this->removeFromParent();
}

void HNAlertView::rightButtonTouched(CCObject *sender, CCControlEvent controlEvent){
	char* inputString = 0;
	if(inputField->isVisible()){
		inputString = (char*)inputField->editBox->getText();
	}
	if(delegate)
		delegate->alertViewButtonTouched(1, inputString);

	removeFromParent();
}